package com.yonyou.crm.sys.modules.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.sys.modules.entity.BlockItemVO;
import com.yonyou.crm.sys.modules.entity.BlockVO;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class BlockBizServiceImpl extends BaseBizServiceImpl<BlockVO>{

	@Autowired
	private CustomIdGeneratorBizServiceImpl customIdGeneratorBizServiceImpl;
	@Autowired
	private BlockItemBizServiceImpl blockItemBizServiceImpl;
	@Autowired
	private ModuleBizServiceImpl moduleBizServiceImpl;
	/**
	 * 获取分组列表
	 * 
	 * @return
	 */
	public List<BlockVO> getBlockList(Long layoutId){

		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("layoutId", layoutId);

		List<BlockVO> blockList = new ArrayList<BlockVO>();
		blockList = dao.selectVoList(BlockVO.class, "ListBlock", paramMap);
		List<Long> customBlocks = new ArrayList<Long>();
		for (BlockVO blockVO : blockList) {
			if (blockVO.getTenantId() != 0) {
				customBlocks.add(blockVO.getId());
			}
		}
		Iterator<BlockVO> it = blockList.iterator();
		while (it.hasNext()) {
			BlockVO block = it.next();
			if (block.getTenantId() == 0
					&& customBlocks.contains(block.getId())) {
				    it.remove();
			} else {
				List<BlockItemVO> blockItemVOs = blockItemBizServiceImpl.getBlockItemList(block.getId());
				block.setItems(blockItemVOs);
			}
		}
		return blockList;
	}
	
	public BlockVO addBlock(ModuleVO moduleVO,BlockVO blockVO) {

		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		Long userId = Long.parseLong(userIdStr);
		blockVO.setTenantId(tenantId);
		blockVO.setId(getNextId());
		Date now = new Date();
		blockVO.setSysCreatedTime(now);
		blockVO.setSysModifiedTime(now);
		blockVO.setSysCreatedUserId(userId);
		blockVO.setSysModifiedUserId(userId);
		blockVO = super.saveVO(blockVO);
		List<BlockItemVO> items = blockVO.getItems();
		for (int i = 0; i < items.size(); i++) {
			BlockItemVO item = items.get(i);
			item.setBlockId(blockVO.getId());
			item = blockItemBizServiceImpl.addBlockItem(moduleVO,item);
		}
		return blockVO;
	}
	
	public void deleteAllBlocks(Long layoutId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("layoutId", layoutId);
		List<Long> blockIds = getBlockIds(layoutId);
		if (blockIds.size()>0) {
			// 先删除分组明细，再删除分组
			blockItemBizServiceImpl.deleteItems(blockIds);
			dao.deleteSql(BlockVO.class, "deleteAllBlocks", paramMap);
		}
	}
	
	/**
	 * 获取分组ID
	 * 
	 * @return
	 */
	private List<Long> getBlockIds(Long layoutId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("layoutId", layoutId);

		List<BlockVO> blockList = new ArrayList<BlockVO>();
		blockList = dao.selectVoList(BlockVO.class, "ListBlock", paramMap);
		List<Long> customBlocks = new ArrayList<Long>();
		for (BlockVO blockVO : blockList) {
			if (blockVO.getTenantId() != 0) {
				customBlocks.add(blockVO.getId());
			}
		}
		Iterator<BlockVO> it = blockList.iterator();
		while (it.hasNext()) {
			BlockVO block = it.next();
			if (block.getTenantId() == 0
					&& customBlocks.contains(block.getId())) {
				    it.remove();
			} 
		}
		List<Long> blockIds = new ArrayList<Long>();
		for (BlockVO blockVO:blockList) {
			blockIds.add(blockVO.getId());
		}
		return blockIds;
	}
	
	private Long getNextId() {
		return customIdGeneratorBizServiceImpl.getId();
	}
}



